var table 
$(document).ready(function() {
	
	var url=sy.bp()+"/franChisee/searchFranchisee.action";
var option ={
		"aLengthMenu":[2,10,15,20], //动态指定分页后每页显示的记录数。
		 "sPaginationType": "full_numbers",
		 "serverSide":true,
		"searching":false,//禁用搜索
		"lengthChange":false, //是否启用改变每页显示多少条数据的控件
		"sort" : "position",  //是否开启列排序，对单独列的设置在每一列的bSortable选项中指定
		"deferRender":true,//延迟渲染
		"bStateSave" : false, //在第三页刷新页面，会自动到第一页
		"iDisplayLength" : 2,  //默认每页显示多少条记录
		"iDisplayStart" : 0,
		"ordering": false,//全局禁用排序
		"dom": '<l<\'#topPlugin\'>f>rt<ip><"clear">',
        "sAjaxSource":url,
		"aoColumns" : [{  //aoColumns设置列时，不可以任意指定列，必须列出所有列。
			"mData" : "id",
			"orderable": false , // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "2%"
        },{
			"mData" : "id",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "6%",
			
		}, {
			"mData" : "name",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%",
			"render":function(data, type, full, meta){  //render改变该列样式,4个参数，其中参数数量是可变的。
		    return	data=data;
		    
		    }
		}, {
			"mData" : "password",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "createtime",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "realname",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "phone",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "state",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "address",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "alliancename",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "email",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "10%"
		},{
			"mData" : "id",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : '',
			"sWidth" : "10%",
		    "render":function(data, type, full, meta){  //render改变该列样式,4个参数，其中参数数量是可变的。
		    	  var topPlugin1=' <button  id="changeOne" class="btn btn-primary" data-id='+data+'>修改</button>' ;
				    return	data='<button id="deleteOne" class="btn btn-danger btn-sm" data-id='+data+'>删 除</button>'+topPlugin1;
		    }}
		],
		"columnDefs" : 
		[{
			"orderable" : false, // 禁用排序
			"targets" : [0], // 指定的列
			"data" : "id",
			"render" : function(data, type, full, meta) {
				  
				return '<input type="checkbox" value="'+ data + '" name="id"/>';
			}
		}],
		"oLanguage" : { // 国际化配置
			"sProcessing" : "正在获取数据，请稍后...",
			"sLengthMenu" : "显示 _MENU_ 条",
			"sZeroRecords" : "没有找到数据",
			"sInfo" : "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
			"sInfoEmpty" : "记录数为0",
			"sInfoFiltered" : "(全部记录数 _MAX_ 条)",
			"sInfoPostFix" : "",
			"sSearch" : "搜索",
			"sUrl" : "",
			"oPaginate" : {
				"sFirst" : "第一页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "最后一页"
			}
		},
		drawCallback: function( settings ) {
	        $('input[name=allChecked]')[0].checked=false;//取消全选状态
	    }
		
	}
	table= $("#franchiseeid").DataTable(option);
});


function tan(){
	//alert(1);
	$('#myModal-add-info').modal('show');
	
}


function franchiseeadd(){
	var url=sy.bp()+"/franChisee/addFranchisee.action";
	//alert($("#franids").val());
	$.ajax({
		url :url,
		type:"post",
		data:{
			"id":$("#franids").val(),
			"name":$("#fnameid").val(),
			"password":$("#passwordid").val(),
			"createtime":$("#createtimeid").val(),
			"realname":$("#realnameid").val(),
			"phone":$("#phoneid").val(),
			"state":$("#stateid").val(),
			"address":$("#addressid").val(),
			"alliancename":$("#nameid").val(),
			"email":$("#emailid").val()
		},
		dataType : "json",
		success : function(data) {
			console.info(data);
			if(data.i==1){
				alert("保存成功");
				
			}else{
				alert("保存失败");
			}
			table.ajax.reload();
			$('#myModal-add-info').modal('hide');
		}
	}); 
}



$(document).delegate('#changeOne','click',function(){
	var id=$(this).data("id");
	//alert(id);
	var url = sy.bp()+"/franChisee/updateFranchisee.action?id="+id;
	$.ajax({
		url:url,
		type:"get",
		success:function(data){
			$("#franid").val(data.record.id),
			$("#franame").val(data.record.name),
			$("#pwid").val(data.record.password),
			$("#crid").val(data.record.createtime),
			$("#realid").val(data.record.realname),
			$("#phonenumberid").val(data.record.phone),
			$("#stateid1").val(data.record.state),
			$("#address").val(data.record.address),
			$("#names").val(data.record.alliancename),
			$("#emailid1").val(data.record.email),
			$('#myModal-update-info').modal('show');
		}
	});
});

function updateFranchisee(){
	var url = sy.bp()+"/franChisee/updateData.action";
	$.ajax({
		url:url,
		type:"post",
		data:{
			"id":$("#franid").val(),
			"name":$("#franame").val(),
			"password":$("#pwid").val(),
			"createtime":$("#crid").val(),
			"realname":$("#realid").val(),
			"phone":$("#phonenumberid").val(),
			"state":$("#stateid1").val(),
			"address":$("#address").val(),
			"alliancename":$("#names").val(),
			"email":$("#emailid1").val()
		},
		dataType:"json",
		success:function(data){
			//console.info(data);
			if(data.i == 1){
				alert("修改成功！");
				
			}else{
				alert("修改失败！");
				table.ajax.reload();
			}
			table.ajax.reload();
			$('#myModal-update-info').modal('hide');
		
			
			
		}
		
	});
	
}

//单选删除
$(document).delegate('#deleteOne','click',function(){
	var id=$(this).data("id");
	//alert(id);
	$('#deleteOneModal').modal('show');
	$("#onedeleteid").val(id);
});

function franChiseeDeletetj(){
	var id = $("#onedeleteid").val();
	//alert(id);
	var url = sy.bp()+"/franChisee/deleteOnce.action?id="+id;
	$.ajax({
		url:url,
		type:"get",
		dataType:"json",
		cache:false,
		success:function(data){
			if(data.i == 1){
				alert("删除成功！");
			}else{
				alert("删除失败！");
			}
			table.ajax.reload();
			$('#deleteOneModal').modal('hide');
		}
	});
}

//批量删除
function deleteAll(){
	var theArray=[];
	$("input[name=id]:checked").each(function() { 
	      theArray.push($(this).val());
	    });
	var ids = theArray.toString();
	if(ids == null || ids==""){
		alert("请至少选择一个");
	}else{
		$("#ids").val(ids);
		$('#deleteAllModal').modal('show');
		}
		
	}


function deleteAlltj(){
	var ids=$("#ids").val();
	//alert(ids);
	$.ajax({
		url:sy.bp()+"/franChisee/deleteAll.action",
		type:"post",
		data:{
			"ids":ids,
		},
		dataType:"json",
		success:function(data){
			if(data.s==1){
				alert("删除成功！");
			}else{
				alert("删除失败！");
			}
			table.ajax.reload();
			$('#deleteAllModal').modal('hide');
		}
	});
	
}

